package com.mxpio.erp.plan.mrp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mxpio.erp.common.enums.ItemEnums;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.entity.ItemLeadTimeProp;
import com.mxpio.erp.common.technology.entity.ItemPlanProp;
import com.mxpio.erp.common.technology.entity.ItemPurchaseProp;
import com.mxpio.erp.common.technology.entity.ItemStoreProp;
import com.mxpio.erp.common.technology.vo.ItemWithSubExtendsVO;
import com.mxpio.erp.plan.mrp.Context;
import com.mxpio.erp.plan.mrp.policy.LrpBomPolicy;
import com.mxpio.erp.plan.mrp.policy.LrpOrderPolicy;
import com.mxpio.erp.plan.mrp.policy.LrpPurchaseOrderPolicy;
import com.mxpio.erp.plan.mrp.policy.LrpSalesOrderPolicy;
import com.mxpio.erp.plan.mrp.policy.LrpStoragePolicy;
import com.mxpio.erp.plan.mrp.service.LrpCoreProvider;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.jpa.JpaUtil;

@Component
public class LrpCoreProviderImpl implements LrpCoreProvider {
	@Autowired
	private LrpOrderPolicy lrpOrderPolicy;
	@Autowired
	private LrpPurchaseOrderPolicy lrpPurchaseOrderPolicy;
	@Autowired
	private LrpSalesOrderPolicy lrpSalesOrderPolicy;
	@Autowired
	private LrpStoragePolicy lrpStoragePolicy;
	@Autowired
	private LrpBomPolicy lrpBomPolicy;
	
	@Override
	public void getBom(Context context) {
		if(lrpBomPolicy != null){
			lrpBomPolicy.run(context);
		}
	}
	
	@Override
	public void getSalesOrderCount(Context context) {
		if(lrpSalesOrderPolicy != null){
			lrpSalesOrderPolicy.run(context);
		}
	}
	
	@Override
	public void getStorage(Context context) {
		if(lrpStoragePolicy != null){
			lrpStoragePolicy.run(context);
		}
	}

	@Override
	public void getOrderCount(Context context) {
		if(lrpOrderPolicy != null){
			lrpOrderPolicy.run(context);
		}
	}

	@Override
	public void getPurchaseOrderCount(Context context) {
		if(lrpPurchaseOrderPolicy != null){
			lrpPurchaseOrderPolicy.run(context);
		}
	}

	@Override
	public void getItem(Context context) {
		
		// 获取物料信息
		Map<String, Item> itemMap = JpaUtil.index(JpaUtil.linq(Item.class).list());

		List<ItemLeadTimeProp> itemLeadTimeProps = JpaUtil.linq(ItemLeadTimeProp.class).equal("propType", ItemEnums.PropType.ITEM.getTypeCode()).list();
		Map<String, ItemLeadTimeProp> itemLeadTimePropMap = JpaUtil.index(itemLeadTimeProps,"code");

		List<ItemPlanProp> itemPlanProps = JpaUtil.linq(ItemPlanProp.class).equal("propType", ItemEnums.PropType.ITEM.getTypeCode()).list();
		Map<String, ItemPlanProp> itemPlanPropMap = JpaUtil.index(itemPlanProps,"code");

		List<ItemStoreProp> itemStoreProps = JpaUtil.linq(ItemStoreProp.class).equal("propType", ItemEnums.PropType.ITEM.getTypeCode()).list();
		Map<String, ItemStoreProp> itemStorePropMap = JpaUtil.index(itemStoreProps,"code");

		List<ItemPurchaseProp> itemPurchaseProps = JpaUtil.linq(ItemPurchaseProp.class).equal("propType", ItemEnums.PropType.ITEM.getTypeCode()).list();
		Map<String, ItemPurchaseProp> itemPurchasePropMap = JpaUtil.index(itemPurchaseProps,"code");
		
		Map<String, ItemWithSubExtendsVO> itemExtendsMap = new HashMap<>();
		Map<String, ItemWithSubExtendsVO> purchaseItemExtendsMap = new HashMap<>();
		for(String itemCode : itemMap.keySet()){
			ItemWithSubExtendsVO extendsVO = new ItemWithSubExtendsVO();
			BeanReflectionUtils.copyProperties(extendsVO, itemMap.get(itemCode));
			extendsVO.setItemLeadTimeProp(itemLeadTimePropMap.get(itemCode));
			extendsVO.setItemPlanProp(itemPlanPropMap.get(itemCode));
			extendsVO.setItemStoreProp(itemStorePropMap.get(itemCode));
			extendsVO.setItemPurchaseProp(itemPurchasePropMap.get(itemCode));
			itemExtendsMap.put(itemCode, extendsVO);
			// 判断是否采购件
			if("2".equals(itemMap.get(itemCode).getItemSource())){
				purchaseItemExtendsMap.put(itemCode, extendsVO);
			}
		}
		
		context.setItems(itemExtendsMap);
		
		context.setPurchaseItems(purchaseItemExtendsMap);
		
	}

}
